package com.gitee.neuray.security.admin.menu.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.gitee.neuray.security.admin.element.biz.AdminElementBiz;
import com.gitee.neuray.security.admin.element.entity.AdminElementEntity;
import com.gitee.neuray.security.admin.menu.entity.AdminMenuEntity;
import com.gitee.neuray.security.admin.menu.biz.AdminMenuBiz;
import com.gitee.neuray.security.admin.menu.vo.AdminMenuVo;
import com.gitee.neuray.security.admin.vo.AdminElement;
import com.gitee.neuray.security.common.msg.ListRestResponse;
import com.gitee.neuray.security.common.msg.ObjectRestResponse;
import com.gitee.neuray.security.common.msg.TableResultResponse;
import com.gitee.neuray.security.common.util.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.gitee.neuray.security.common.rest.BaseController;

import java.util.List;
import java.util.Map;


/**
 * 菜单管理
 *
 * @author zhoyou
 * @email ${email}
 * @date 2020-03-12 05:43:38
 */
@RestController
@RequestMapping("pmMenu")
public class AdminMenuController extends BaseController<AdminMenuBiz, AdminMenuEntity>{
    @Autowired
    private AdminElementBiz elementBiz;

    @Autowired
    private AdminMenuBiz adminMenuBiz;

    /**
     * 根据menuId获取element
     * @param menuId
     * @return
     */
    @RequestMapping(value = "/element",method = RequestMethod.GET)
    @ResponseBody
    public ListRestResponse<List<AdminElement>> listMenuElement(@RequestParam("menuId") String menuId){
        List<AdminElement> adminElements = elementBiz.listMenuElement(menuId);
        return new ListRestResponse("",adminElements.size(),adminElements);
    }

    /**
     * 给menu添加element或修改element
     * @return
     */
    @RequestMapping(value = "/element",method = RequestMethod.PUT)
    @ResponseBody
    public ObjectRestResponse modifyMenuElement(@RequestBody Map<String,Object> map){
        String menuId = (String) map.get("menuId");
        List<AdminElement> elementList = JSON.parseArray(JSON.toJSONString(map.get("elements")),AdminElement.class) ;
        elementBiz.modifyMenuElement(menuId,elementList);
        return new ObjectRestResponse().rel(true);
    }

    @RequestMapping(value = "/buildTree",method = RequestMethod.GET)
    @ResponseBody
    public ListRestResponse<List<AdminElement>> buildTree(){
        List<AdminMenuEntity> ret = adminMenuBiz.buildTree();
        return new ListRestResponse("",ret.size(),ret);
    }

    /**
     * 给menu添加element或修改element
     * @return
     */
    @RequestMapping(value = "/insertMenu",method = RequestMethod.POST)
    @ResponseBody
    public ObjectRestResponse insertMenu(@RequestBody AdminMenuVo adminMenuVo){
        adminMenuBiz.insertMenuAndElements(adminMenuVo);
        return new ObjectRestResponse().rel(true);
    }

    /**
     * 修改菜单
     * @return
     */
    @RequestMapping(value = "/modifyMenu",method = RequestMethod.PUT)
    @ResponseBody
    public ObjectRestResponse modifyMenu(@RequestBody AdminMenuVo adminMenuVo){
        adminMenuBiz.updateMenuAndElements(adminMenuVo);
        return new ObjectRestResponse().rel(true);
    }

    /**
     * 删除菜单
     * @return
     */
    @RequestMapping(value = "/deleteMenu",method = RequestMethod.DELETE)
    @ResponseBody
    public ObjectRestResponse deleteMenu(@RequestBody AdminMenuEntity adminMenuEntity){

        //删除菜单
        adminMenuBiz.delete(adminMenuEntity);

        //删除菜单下element
        AdminElementEntity adminElement = new AdminElementEntity();
        adminElement.setMenuId(adminMenuEntity.getId());
        elementBiz.delete(adminElement);

        return new ObjectRestResponse().rel(true);
    }

    /**
     * 根据menuId获取menu
     * @param params
     * @return
     */
    @Override
    @RequestMapping(value = "/page",method = RequestMethod.GET)
    @ResponseBody
    public TableResultResponse<AdminMenuEntity> page(@RequestParam Map<String, Object> params){
        Query query = new Query(params);
        AdminMenuEntity adminMenuEntity = JSONObject.parseObject(JSONObject.toJSONString(params),AdminMenuEntity.class);

        return adminMenuBiz.page(query,adminMenuEntity);
    }

    /**
     * 根据menuid获取菜单
     * @param id
     * @return
     */
    @RequestMapping(value = "/_search/{id}", method = RequestMethod.GET)
    @ResponseBody
    public ObjectRestResponse<AdminMenuVo> getMenu(@PathVariable String id) {
        ObjectRestResponse<AdminMenuVo> entityObjectRestResponse = new ObjectRestResponse<>();
        AdminMenuVo adminMenuVo = baseBiz.getMenu(id);
        entityObjectRestResponse.data(adminMenuVo).rel(true);
        return entityObjectRestResponse;
    }

}
